package com.example.algorithm.array;

import lombok.Builder;
import lombok.Data;

import java.util.Arrays;

@Data
@Builder
public class Array<E> {
    private int[] data;
    private int size;

    public static void main(String[] args) {
        Array array = Array.builder().build();
        array.setCapacity(20);
        for (int i = 0; i < 10; i++) {
            array.addLast(i);
        }
        array.addIndex(1, 100);
        System.out.println("array = " + array.contains(100));
        System.out.println("array = " + array.contains(11));
        System.out.printf("\n" + array.toString());
        System.out.println("array = " + array.findIndex(100));
    }

    public void setIndex(int index, int e) {
        data[index] = e;

    }

    public void setCapacity(int capacity) {
        data = new int[capacity];
        this.size = 0;
    }

    public void addLast(int e) {
        addIndex(size, e);
    }

    public void addFirst(int e) {
        addIndex(0, e);
    }

    public void addIndex(int index, int e) {
        if (size == data.length) {
            throw new RuntimeException();
        }
        if (index < 0 || index > size)
            throw new RuntimeException();
        for (int i = size; i > index; i--) {
            data[i] = data[i - 1];
        }
        data[index] = e;
        size++;
    }

    public int getIndex(int index) {
        return data[index];
    }

    public boolean contains(int e) {
        return Arrays.stream(data).anyMatch(value -> value == e);
    }

    public int findIndex(int e) {
        for (int i = 0; i < data.length; i++) {
            if (e == data[i])
                return i;
        }
        return -1;
    }
}
